package mvc.views.mediators
{
	import alternativa.gui.data.DataProvider;
	
	import com.asual.swfaddress.SWFAddress;
	
	import flash.external.ExternalInterface;
	
	import mvc.AppFacade;
	import mvc.Notifications;
	import mvc.views.AppMainView;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * swfAddress控制类
	 * @author xiling
	 */
	public class MediatorSwfAddress extends Mediator implements IMediator
	{
		public static const NAME:String = "MediatorSwfAddress";
		public function MediatorSwfAddress(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			initSwfAddress();
		}
		
		override public function listNotificationInterests():Array
		{
			// TODO Auto Generated method stub
			return [
				Notifications.MEDIATOR_GOTO_CLIENT_URL
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			// TODO Auto Generated method stub
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
				case Notifications.MEDIATOR_GOTO_CLIENT_URL:
					var url:String = notification.getBody() as String;
					SWFAddress.setValue(url);//触发onSwfAddressChange
					break;
			}
		}
		
		private function initSwfAddress():void
		{
			//ExternalInterface.call('alert','sdf');
			SWFAddress.setStrict(false);
			SWFAddress.onChange = onSwfAddressChange;
		}
		
		private function onSwfAddressChange():void
		{
			
			var value:String = SWFAddress.getValue();
			var path:String = SWFAddress.getPath();
			var id:Object = SWFAddress.getParameter('id');
			var title:String = 'SWFAddress Website';	
			var names:Array = SWFAddress.getPathNames();
			//names[i]地址中以/分割的数组,以user/info为例，得到的notification是MEDIATOR_SWITCH_TO_USER_INFO
			if(names.length>=2)
			{
				var mediatorName:String = names[0].toUpperCase();
				var mediatorAction:String = names[1].toUpperCase();
				sendNotification(Notifications.MEDIATOR_SWITCH_SCENE,mediatorName+'_'+mediatorAction);
				AppFacade.getInstance().sendNotification(Notifications['MEDIATOR_SWITCH_TO_'+mediatorName+'_'+mediatorAction]);
				id = SWFAddress.getParameter('id');
				if (typeof id != 'undefined') {
					title += ' / ' + id; 
				}
				SWFAddress.setTitle(title);						
			}
		}
		
		
	}
}